
const mongoServer = {
     /**
   * @description: 添加数据
   * @param {string} user_id 用户
   * @return {number} 统计数量 
   */
  addCount: async (db, data) => {
    const { user_id } = data
    try {
      let date = new Date();
      let shareCount = await db.mongo.count('Fission_table', { 'user_id': user_id });
      let addCount;
      if (shareCount[0]) {
        addCount = await await db.mongo.updateMany('Fission_table', { 'user_id': user_id }, { $inc: { "num": 1 }, $set: { "gmt_modified": date } });
      } else {
        addCount = await db.mongo.insertOne('Fission_table', { 'user_id': user_id, 'num': 1 });
      }
      return {success: true, data: {addCount}, errorCode: "", errorMessage: ""}
    } catch (err) {
      return {success: false, data: {}, errorCode: "501", errorMessage: err}
    }
  },
       /**
   * @description: 添加数据
   * @param {string} user_id 用户
   * @return {number} 统计数量 
   */
  getCount: async (db, data) => {
    const { user_id } = data
    try {
      let FissionNum = await db.mongo.find('Fission_table', data, null, 0, 1);
      return {success: true, data: {num: FissionNum[0] && FissionNum[0].num ? FissionNum[0].num : 0 }, errorCode: "", errorMessage: ""}
    } catch (err) {
      return {success: false, data: {}, errorCode: "501", errorMessage: err}
    }
  },
   /**
   * @description: 添加数据
   * @param {string} user_id 用户
   * @param {string} share_user_id 分享人 
   * @param {string} share_avatar  分享人头像
   * @return {number} 统计数量 
   */
  addLog: async (db, data) => {
    const { user_id,share_user_id,share_avatar } = data
    try {
      let date = new Date();
      let sqlData= {
        "gmt_create": date,
         "gmt_modified": date,
         ...data
      } 
      console.log("============================",sqlData)
      let fissionLog = await db.mongo.insertOne('fission_log',sqlData);
       console.log("===========fissionLog=================",fissionLog)
      return {success: true, data: {fissionLog:fissionLog}, errorCode: "", errorMessage: ""}
    } catch (err) {
      return {success: false, data: {}, errorCode: "501", errorMessage: err}
    }
  },

   /**
   * @description: 添加数据
   * @param {object} user_id 用户
   * @param {object} share_user_id 分享人 
   * @param {date} start_time  开始时间
   * @return {date} end_time   结束时间
   */
  getLog: async (db, data) => {
    const { user_id,start_time = null, end_time = null } = data
    try {
      let sqlData = {share_user_id: user_id }
      if (start_time) sqlData.gmt_create = { "$gte": start_time };
      if (end_time) sqlData.gmt_create = { "$lte": start_time };
      if (start_time && end_time) sqlData.gmt_create = { "$gte": start_time, "$lte": end_time }
      console.log("===========sqlData===sqlData===========",sqlData)
      let FissionLog = await db.mongo.find('fission_log', sqlData);
      return {success: true, data: {FissionLog:FissionLog}, errorCode: "", errorMessage: ""}
    } catch (err) {
      return {success: false, data: {}, errorCode: "501", errorMessage: err}
    }
  },

   /**
   * @description: 添加数据
   * @param {object} user_id 用户
   * @param {object} share_user_id 分享人 
   * @param {date} start_time  开始时间
   * @return {date} end_time   结束时间
   */
  getLogCount: async(db,data)=>{


    const { user_id, share_user_id=null,start_time = null, end_time = null } = data
    let sqlData = {share_user_id: user_id }
    if(share_user_id) sqlData.user_id = share_user_id;
    if (start_time) sqlData.gmt_create = { "$gte": start_time };
    if (end_time) sqlData.gmt_create = { "$lte": start_time };
    if (start_time && end_time) sqlData.gmt_create = { "$gte": start_time, "$lte": end_time }
    console.log('==========sqlData=========',sqlData)
    try {
      let fissionCout = await db.mongo.count('fission_log',sqlData);
        console.log('==========fissionCout=========',fissionCout)
      return {success: true, data: {count: fissionCout}, errorCode: "", errorMessage: ""}
    } catch (err) {
      return {success: false, data: {}, errorCode: "501", errorMessage: err}
    }
  }
};

// export default mongoServer;
exports.mongoServer=  mongoServer;